package edu.towson.greenteam.order;

import edu.towson.greenteam.Iterator;


public abstract class OrderItr implements Iterator<Order> {
	public Orders allOrders;
	public boolean hasNextItem = false;
	public Order nextOrder = null;
	public Order lastOrder = null;
	protected int index = 0;


	public abstract boolean canProceed(Order testOrder);

	public boolean hasNext() {
		return hasNextItem;
	}

	public Order next() {
		Order returnOrder = nextOrder;
		calcNext();
		return returnOrder;
	}

	protected void calcNext() {
		hasNextItem = false;
		while (!hasNextItem && (allOrders.get(index) != null)) {
			Order tempOrder = allOrders.get(index);
			index++;
			if (tempOrder != null) {
				// if it isn't discounted go to the next one
				if (canProceed(tempOrder)) {
					if (lastOrder == nextOrder) {
						lastOrder = null;
					}
					else {
						lastOrder = nextOrder;
					}
					nextOrder = tempOrder;
					hasNextItem = true;
				} else {
					calcNext();
				}
			}
		}
	}

	public void remove() {
		throw new UnsupportedOperationException(
				"Does not implement the remove method");
	}

	public boolean hasPrevious() {
		return (lastOrder == null);
	}

	public Order previous() {
		return lastOrder;
	}
}
